package com.fzn.springboot_jsp_shiro.shiro.realms;

import com.fzn.springboot_jsp_shiro.dao.UserDao;
import com.fzn.springboot_jsp_shiro.entity.Perms;
import com.fzn.springboot_jsp_shiro.entity.Role;
import com.fzn.springboot_jsp_shiro.entity.User;
import com.fzn.springboot_jsp_shiro.service.UserService;
import com.fzn.springboot_jsp_shiro.shiro.salt.MyByteSource;
import com.fzn.springboot_jsp_shiro.utils.ApplicationContextUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author fzn
 * @date 2022/3/19
 * @apiNote
 *  自定义Realm
 */
public class CustomerRealm extends AuthorizingRealm {
    // 处理授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取身份信息
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
        System.out.println("调用授权验证"+primaryPrincipal);
        // 根据主身份信息获取角色和权限信息
        //if ("fzn".equals(primaryPrincipal)) {
        //    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //    // 增加角色
        //    simpleAuthorizationInfo.addRole("admin");
        //    simpleAuthorizationInfo.addRole("user");
        //    // 增加权限字符串
        //    simpleAuthorizationInfo.addStringPermission("user:find:*");
        //    simpleAuthorizationInfo.addStringPermission("user:update:*");
        //
        //    return simpleAuthorizationInfo;
        //}
        UserService userService = (UserService) ApplicationContextUtils.getBean("userServiceImpl");
        User user = userService.findRoleByUserName(primaryPrincipal);
        // 赋值授权角色
        if (!CollectionUtils.isEmpty(user.getRoles())) {
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            user.getRoles().forEach(role -> {
                // 加入角色
                simpleAuthorizationInfo.addRole(role.getName());
                // 权限信息
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)){
                    // 加入权限
                    perms.forEach(perm -> {
                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                    });
                }
            });
            return simpleAuthorizationInfo;
        }
        return null;
    }

    // 处理认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("==============");
        // 拿到用户信息
        String principal = (String) token.getPrincipal();
        // 在工厂中获取service对象
        UserService userService = (UserService) ApplicationContextUtils.getBean("userServiceImpl");
        //System.out.println(userService);
        // 查询出的user
        User user = userService.findByUserName(principal);
        if (!ObjectUtils.isEmpty(user)) {
            //return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(), ByteSource.Util.bytes(user.getSalt()),this.getName());
            return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),new  MyByteSource(user.getSalt()),this.getName());
        }
        return null;
    }
}
